import { _decorator, Component, Label, Node } from 'cc';
import { LayerBase } from './LayerBase';
import { UILayerMgr } from './UILayerMgr';
import { UserInfo } from './UserInfo';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { gameMgr } from '../mainScene/gameMgr';
import { convertNumber, keepOneDecimalFullFomatFloat, randomNum } from '../utils/utils';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('EnhancePetPanel')
export class EnhancePetPanel extends LayerBase {
   
    @property(Label)
    nameLabel:Label;
    @property(Label)
    expendLabel:Label;
    @property(Node)
    leftRoot:Node;
    @property(Node)
    rightRoot:Node;
    @property(Label)
    coinLabel:Label;
    @property(Label)
    crystalLabel:Label;

    pet;
    init(somedData?: any): void {
        this.pet=somedData;
        this.nameLabel.string=somedData.petTp.name;
        this.coinLabel.string=convertNumber(UserInfo.coinMun);
        this.crystalLabel.string=convertNumber(UserInfo.crystalMun);
        this.leftRoot.getChildByName("levelValue").getComponent(Label).string=somedData.petLevel+""
        this.leftRoot.children[0].getChildByName("attackLabel").getComponent(Label).string=somedData.attackAddition+"";

        this.rightRoot.getChildByName("levelValue").getComponent(Label).string=(somedData.petLevel+1).toString()
        this.rightRoot.children[0].getChildByName("attackLabel").getComponent(Label).string=somedData.nextLevelAttackAddition+"";

        this.expendLabel.string="消耗金币："+somedData.EnhanceCost;
    }

    onclickEnhanceBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        let petClass = UserInfo.hasPetDic.get(this.pet.petId)
        if (UserInfo.coinMun >= petClass.EnhanceCost) {
            UserInfo.coinMun -= petClass.EnhanceCost

            let randCount=randomNum(0,99);
            if (petClass.getEnhanceSucceedProbability>randCount) {//成功强化
                StartSceneGameMgr.ins.createWidowTip("强化成功")
                petClass.petLevel += 1;
               
            }
            else{//强化失败
                StartSceneGameMgr.ins.createWidowTip("强化失败")
            }

            StartSceneGameMgr.ins.saveGameDate();
            UILayerMgr.getIns.resfreshUI("enhancePetPanel", petClass);
            UILayerMgr.getIns.resfreshUI("desPetPanel", {pet:petClass,isClickTopContent:true});
            UILayerMgr.getIns.resfreshUI("petPanel");

        }
        else {
            StartSceneGameMgr.ins.createWidowTip("金币不足")
        }

    }
    
    addCoinBtn(){
        UILayerMgr.getIns.openUILayer("getCoinCrystalPanel");
    }

    renewCoinLabel(){
        this.coinLabel.string=convertNumber(UserInfo.coinMun);
    }

    renewCrystalLabel(){
        this.crystalLabel.string=convertNumber(UserInfo.crystalMun);
    }

}


